package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Description:
 * @Auther:ccc
 * @Date:2022/11/4 14:42
 */
@RestController
@RequestMapping("/testRedis")
public class RedisController {
    @Autowired
    RedisService service;

    private static String key = "testKey";

    @GetMapping("/putA")
    public String testPutHash(@RequestParam String field, @RequestParam String value) {
        service.putHash(key, field, value);
        return "存值成功！";
    }

    @GetMapping("/putB")
    public String testPutHashWithTime(@RequestParam String field, @RequestParam String value) {
        service.putHash(key, field, value, 60L);
        return "存值带时间成功！";
    }

    @GetMapping("/getA")
    public String testGetHash(@RequestParam String field) {
        Object hash = service.getHash(key, field);
        return JSONObject.toJSONString(hash);
    }
    @GetMapping("/getB")
    public String testGetHashAll() {
        Map<String, Object> hashAll = service.getHashAll(key);
        return JSONObject.toJSONString(hashAll);
    }
}
